package com.webapp.helper;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.webapp.view.BaseContants;

public class DataStringUtils {

	/**
     * String转换成double 保留N位小数。
     * @param a
     * @return
     */
    public static double stringToDouble(String a){
    	double b = Double.valueOf(a);
    	DecimalFormat df = new DecimalFormat("#.000");//此为保留1位小数，若想保留2位小数，则填写#.00  ，以此类推
    	String temp = df.format(b);
    	b = Double.valueOf(temp);
    	return b;
    }
	
	/**
	 * 字节转字符串
	 * @param src
	 * @return
	 */
	public static String bytesToHexString(byte[] src){       
        StringBuilder stringBuilder = new StringBuilder();       
        if (src == null || src.length <= 0) {       
            return null;       
        }       
        for (int i = 0; i < src.length; i++) {       
            int v = src[i] & 0xFF;       
            String hv = Integer.toHexString(v);       
            if (hv.length() < 2) {       
                stringBuilder.append(0);       
            }       
            stringBuilder.append(hv);       
        }       
        return stringBuilder.toString();       
    }
	
	/**
	 * 文件转流
	 * @param file
	 * @return
	 */
	public static byte[] fileConvertToByteArray(File file) {
		byte[] data = null;
		try {
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			int len;
			byte[] buffer = new byte[1024];
			while ((len = fis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}

			data = baos.toByteArray();

			fis.close();
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}
	
	/**
	 * 格式化数据
	 * @param hexString
	 * @param i
	 * @return
	 */
	public static String getString(String hexString, int i) {
		if (i == 4) {
			if (hexString.length() == 1) {
				return "000" + hexString;
			}else if (hexString.length() == 2) {
				return "00" + hexString;
			}else if (hexString.length() == 3) {
				return "0" + hexString;
			}else if (hexString.length() == 4) {
				return hexString;
			}
		}
		if (i == 8) {
			if (hexString.length() == 1) {
				return "0000000" + hexString;
			}else if (hexString.length() == 2) {
				return "000000" + hexString;
			}else if (hexString.length() == 3) {
				return "00000" + hexString;
			}else if (hexString.length() == 4) {
				return "0000" + hexString;
			}else if (hexString.length() == 5) {
				return "000" + hexString;
			}else if (hexString.length() == 6) {
				return "00" + hexString;
			}else if (hexString.length() == 7) {
				return "0" + hexString;
			}else if (hexString.length() == 8) {
				return hexString;
			}
		}
		return null;
	}
	
	/**
	 * 字符串转2进制数组
	 * @param data
	 * @return
	 */
	public static int[] getB(String data) {
		Integer num = Integer.parseInt(data, 16);
		String a = Integer.toBinaryString(num);
		int[] s = new int[] { 0, 0, 0, 0, 0, 0,0 ,0,0};
		if (a.length() == 1) {
			s[0] = Integer.valueOf(a);
		}
		if (a.length() == 2) {
			s[0] = Integer.valueOf(a.substring(1,2));
			s[1] = Integer.valueOf(a.substring(0, 1));
		}
		if (a.length() == 3) {
			s[0] = Integer.valueOf(a.substring(2, 3));
			s[1] = Integer.valueOf(a.substring(1, 2));
			s[2] = Integer.valueOf(a.substring(0, 1));
		}
		if (a.length() == 4) {
			s[0] = Integer.valueOf(a.substring(3, 4));
			s[1] = Integer.valueOf(a.substring(2,3));
			s[2] = Integer.valueOf(a.substring(1, 2));
			s[3] = Integer.valueOf(a.substring(0, 1));
		}
		if (a.length() == 5) {
			s[0] = Integer.valueOf(a.substring(4,5));
			s[1] = Integer.valueOf(a.substring(3, 4));
			s[2] = Integer.valueOf(a.substring(2, 3));
			s[3] = Integer.valueOf(a.substring(1, 2));
			s[4] = Integer.valueOf(a.substring(0, 1));
		}
		if (a.length() == 6) {
			s[0] = Integer.valueOf(a.substring(5, 6));
			s[1] = Integer.valueOf(a.substring(4,5));
			s[2] = Integer.valueOf(a.substring(3, 4));
			s[3] = Integer.valueOf(a.substring(2, 3));
			s[4] = Integer.valueOf(a.substring(1, 2));
			s[5] = Integer.valueOf(a.substring(0, 1));
		}
		if (a.length() == 7) {
			s[0] = Integer.valueOf(a.substring(6, 7));
			s[1] = Integer.valueOf(a.substring(5, 6));
			s[2] = Integer.valueOf(a.substring(4,5));
			s[3] = Integer.valueOf(a.substring(3, 4));
			s[4] = Integer.valueOf(a.substring(2, 3));
			s[5] = Integer.valueOf(a.substring(1, 2));
			s[6] = Integer.valueOf(a.substring(0, 1));
		}
		return s;
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public String convertStringToHex(String str) {
		char[] chars = str.toCharArray();
		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			hex.append(Integer.toHexString((int) chars[i]));
		}
		return hex.toString();
	}
	
	/**
	 * 带符号16进制转小数
	 * @param index
	 * @param _d
	 * @return
	 */
	public static String getData(String index, String _d,int sub) {
		try {
			int _index = Integer.parseInt(index, 16);
			BigInteger bigInt = new BigInteger(_d,16);
			String b = Integer.toBinaryString(bigInt.intValue());
			String[] arrs = b.split("");
			String[] arr = new String[32];
			int i = 0;
			for (String s : arrs) {
				if (s != null && !"".equals(s)) {
					arr[i] = s;
					i++;
				}
			}
			String c = "";
			BigInteger e = new BigInteger(b,2);
			if (_index > 8) {
				for (String d : arr) {
					if (d != null && !"".equals(d)) {
						if (d.equals("1")) {
							c += "0";
						} else {
							c += "1";
						}
					}
				}
				e =  new BigInteger(String.valueOf((new BigInteger(c,2).intValue()+1)*-1));
			} else {
				e = new BigInteger(_d,16);
			}
			Double r = Double.valueOf(e.intValue())/sub;
			BigDecimal f = new BigDecimal(r);
			double g = f.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
			DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
			String result = decimalFormat.format(g);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 16进制转字符串
	 * 
	 * @param hex
	 * @return
	 */
	public static String convertHexToString(String hex) {
		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();
		for (int i = 0; i < hex.length() - 1; i += 2) {
			String output = hex.substring(i, (i + 2));
			int decimal = Integer.parseInt(output, 16);
			sb.append((char) decimal);
			temp.append(decimal);
		}
		return sb.toString();
	}

	/**
	 * 检测数据打包
	 * 
	 * @param log
	 * @param nw
	 * @return
	 */
	public static String getPostJson(Object log, String deviceType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(BaseContants.DEVICE_TYPE_STR, deviceType);
		map.put(BaseContants.DEVICE_DATA_STR, log);
		return JSONObject.toJSONString(map);
	}

	/**
	 * 雨量计值解析
	 * 
	 * @param str
	 * @return
	 */
	public static String getRainHex(String str) {
		return str.substring(6, 8) + str.substring(4, 6) + str.substring(2, 4) + str.substring(0, 2);
	}

	/**
	 * hex直转Double
	 * 
	 * @param s
	 * @return
	 */
	public static String hexToFloat(String s) {
		BigInteger big = new BigInteger(s, 16);
		Float z = Float.intBitsToFloat(big.intValue());
		Double r = Double.valueOf(z);
		BigDecimal f = new BigDecimal(r);
		double g = f.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		DecimalFormat decimalFormat = new DecimalFormat("###################.#######");
		String result = decimalFormat.format(g);
		return result;
	}
	
	public static Double hexToDouble(String s) {
		BigInteger big = new BigInteger(s, 16);
		Float z = Float.intBitsToFloat(big.intValue());
		Double r = Double.valueOf(z);
		BigDecimal f = new BigDecimal(r);
		double g = f.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		return g;
	}

	public static int[] getBAcc(String data) {
		Integer num = Integer.parseInt(data, 16);
		String a = Integer.toBinaryString(num);
		int[] s = new int[] { 0, 0, 0, 0, 0, 0 };
		if (a.length() == 1) {
			s[5] = Integer.valueOf(a);
		}
		if (a.length() == 2) {
			s[4] = Integer.valueOf(a.substring(0, 1));
			s[5] = Integer.valueOf(a.substring(1, 2));
		}
		if (a.length() == 3) {
			s[3] = Integer.valueOf(a.substring(0, 1));
			s[4] = Integer.valueOf(a.substring(1, 2));
			s[5] = Integer.valueOf(a.substring(2, 3));
		}
		if (a.length() == 4) {
			s[2] = Integer.valueOf(a.substring(0, 1));
			s[3] = Integer.valueOf(a.substring(1, 2));
			s[4] = Integer.valueOf(a.substring(2, 3));
			s[5] = Integer.valueOf(a.substring(3, 4));
		}
		if (a.length() == 5) {
			s[1] = Integer.valueOf(a.substring(0, 1));
			s[2] = Integer.valueOf(a.substring(1, 2));
			s[3] = Integer.valueOf(a.substring(2, 3));
			s[4] = Integer.valueOf(a.substring(3, 4));
			s[5] = Integer.valueOf(a.substring(4, 5));
		}
		if (a.length() == 6) {
			s[0] = Integer.valueOf(a.substring(0, 1));
			s[1] = Integer.valueOf(a.substring(1, 2));
			s[2] = Integer.valueOf(a.substring(2, 3));
			s[3] = Integer.valueOf(a.substring(3, 4));
			s[4] = Integer.valueOf(a.substring(4, 5));
			s[5] = Integer.valueOf(a.substring(5, 6));
		}
		return s;
	}

}
